<?php

namespace App\Repository\Auth;

use App\Model\Auth\Admin;
use App\Model\Auth\Menu;
use App\Repository\IRepository;
use Hyperf\Contract\LengthAwarePaginatorInterface;
use Hyperf\Database\Model\Builder;

class AdminRepository extends IRepository
{
    public function __construct(
        protected readonly Admin $model
    )
    {
    }

    public function handleSearch(Builder $query, array $params): Builder
    {

        return $query->when(isset($params['nickname']) && $params['nickname'], function ($query) use ($params) {
                return $query->where("nickname", "like", "%{$params['nickname']}%");
            })
            ->when(isset($params['account']) && $params['account'], function ($query) use ($params) {
                return $query->where("account", "like", "%{$params['account']}%");
            })
            ->when(isset($params['id']) && $params['id'], function ($query) use ($params) {
                return $query->where("id", $params['id']);
            })
            ->when(isset($params['status']) && $params['status'], function ($query) use ($params) {
                return $query->where("status", $params['status']);
            })
            ->select(['id','nickname','account','avatar','status','created_at','updated_at']);
    }
    public function handlePage(LengthAwarePaginatorInterface $paginator): array
    {
        $list = $paginator->items();

        return  [
            'total' => $paginator->total(),
            'per_page' => $paginator->perPage(),
            'current_page' => $paginator->currentPage(),
            'list' => $list,
        ];
    }
}